require([
  "jquery",
  "ko",
  "bootstrap",
  "jquery-select2",
  "jquery-gritter",
  "jquery-blockui",
  "jquery-slimscroll",
  "akcy"
], function ($, ko) {
  var akcy = arguments[arguments.length - 1];
  akcy.ui.init();

  akcy.ajaxget("monitor", 'config/SysDicInfoList').done(function (dic) {

    var tankArea = akcy.tree().options({
      dataQueryUrl: 'config/TankAreaList'
    }).optionsExtend({
      appId: "monitor",
      searched: function (data) {
        pageForm.getInstance().tabkAreaList(data);

      },
      nodeClicked: function (data, e) {
        table.getInstance().tankAreaId(data.tankAreaId);
        pageForm.getInstance().reset();
        table.getInstance().tankAreaName(data.tankAreaAlias);
        table.load();
        e.stopPropagation();
      }
    }).bind("tankArea");

    var pageForm = akcy.form().options({
      fields: [{
        field: 'tankId',
        value: ko.observable('')
      }, {
        field: 'tankType',
        value: ko.observable()
      }, {
        field: 'tankAreaId',
        value: ko.observable()
      }, {
        field: 'stkhsFlag',
        value: ko.observable()
      }, {
        field: 'fltPlatWgt',
        value: ko.observable()
      }, {
        field: 'tankHgt',
        value: ko.observable()
      }, {
        field: 'htPretTank',
        value: ko.observable()
      }, {
        field: 'fltPlatPerhgt',
        value: ko.observable()
      }, {
        field: 'tankTotlCuba',
        value: ko.observable()
      }, {
        field: 'fltTipLst',
        value: ko.observable()
      }, {
        field: 'maxTankHgt',
        value: ko.observable()
      }, {
        field: 'minTankHgt',
        value: ko.observable()
      }, {
        field: 'maxSafeTankhigh',
        value: ko.observable()
      }, {
        field: 'minSafeTankhigh',
        value: ko.observable()
      }, {
        field: 'safeTanksaveUpper',
        value: ko.observable()
      }, {
        field: 'rsid',
        value: ko.observable()
      }, {
        field: 'tankCode',
        value: ko.observable()
      }, {
        field: 'tankName',
        value: ko.observable()
      }, {
        field: 'tankAlias',
        value: ko.observable()
      }, {
        field: 'temperatureTagcode',
        value: ko.observable()
      }, {
        field: 'pressureTagcode',
        value: ko.observable()
      }, {
        field: 'safeTanksaveLower',
        value: ko.observable()
      }, {
        field: 'levelTagcode',
        value: ko.observable()
      }, {
        field: 'isDelete',
        value: ko.observable()
      }],
      dataAddUrl: 'config/AddOrModifyTankInfo',
      dataUpdateUrl: 'config/AddOrModifyTankInfo',
      dataDeleteUrl: 'config/DeleteTank'

    }).optionsExtend({
      appId: 'monitor',
      keyId: 'tankCode',

      added: function (data, rst) {
        pageForm.getInstance().reset();
        pageForm.getInstance().load(rst);
        table.load();
        akcy.alert.success("新增成功！");
      },
      updated: function () {
        table.load();
        // pageForm.getInstance().reset();
        akcy.alert.success("保存成功！");
      },
      deleted: function (data, rst) {
        if (rst.status) {
          table.load();
          pageForm.getInstance().reset();
          akcy.alert.success("删除成功！");
        } else {

          akcy.alert.error(rst.msg)
        }
      },
      tankTypeList: ko.observableArray(dic),
      tabkAreaList: ko.observableArray()
    }).bind("pageinfo");
    var table = akcy.table().options({
      fields: [{
        field: 'tankAreaId',
        value: ko.observable(0)
      }],
      headers: [{
        text: '罐名称',

        width: '10%'
      }, {
        text: '罐类型',

        width: '11%'
      }, {
        text: '罐编码',

        width: '5%'
      }, {
        text: '罐区',

        width: '11%'
      }],
      // defaultOrderBy: "U_ID",
      dataQueryUrl: 'config/TankInfoList'
    }).optionsExtend({
      appId: "monitor",
      tankAreaName: ko.observable(),
      branches: ko.observableArray([]),
      nodes: ko.observableArray([]),
      searched: function (data) {
        tankArea.load()
      },
      adduser: function () {
        form.getInstance().reset();

      },
      edituser: function (data) {
        actionTable.getInstance().etlid(pageForm.getInstance().id());
        actionTable.load();
      },
      isCheckedAll: ko.observable(false),
      checkedAll: function () {
        var array = [];
        if (table.getInstance().isCheckedAll() == false) {
          table.getInstance().isCheckedAll(true);
          $(table.getInstance().recordSet()).each(function () {
            array.push($(this)[0].tankId);
          });
        } else {
          table.getInstance().isCheckedAll(false);
        }
        table.getInstance().checkedUser(array);
      },
      checkedUser: ko.observableArray(),
      checkService: function (data) {
        if (!table.getInstance().checkedUser().contains(data.id))
          table.getInstance().checkedUser().insert(0, data.id);
      },
      rowclick: function (data) {
        pageForm.getInstance().reset();
        pageForm.load(data);

      },
      exece: function (data) {
        prameTable.getInstance().etlid(data.id);
        prameTable.getInstance().serviceData(data);
        prameTable.load();
        akcy.showmodal("modal-user");
      },
      addService: function () {
        pageForm.getInstance().reset();
      },
      deleteService: function () {
        akcy.confirm('确认删除吗？', function () {
          akcy.ajaxdelete('config/DeleteTank/' + table.getInstance().checkedUser().join(','), {}).done(function () {
            akcy.alert.success("删除成功！");
            table.load();
            prameTable.getInstance().etlid(0);
            prameTable.load();
          })
        })
      }
    }).bind("tankList").load();
  })

});